package thread;

import org.w3c.dom.ls.LSOutput;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

class MyThread1 extends Thread {
    @Override
    public void run() {
        System.out.println("开启线程的第一种方式：继承Thread,重写run方法");
    }
}

class MyThread2  implements Runnable {

    @Override
    public void run() {
        System.out.println("开启线程的第二种方式：实现Runnable,重写run方法");
    }
}

public class Test01 {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        //1.继承Thread,重写run方法
        Thread thread1 = new MyThread1();
        System.out.println("main线程");
        thread1.start();

        //2.实现Runnable,重写run方法
       Thread thread2 = new Thread(new MyThread2());
       thread2.start();

        //3.继承Thread使用匿名内部类的方式
        Thread thread3 = new Thread(){
            @Override
            public void run() {
                System.out.println("开启线程的第三种方式：继承Thread使用匿名内部类的方式，重写run方法");
            }
        };
        thread3.start();

        //4.实现Runnable，使用匿名内部类
        Thread thread4 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("实现Runnable，使用匿名内部类,重写run方法");
            }
        });
        thread4.start();

        //5.直接使用lambda表达式
        Thread thread5 = new Thread(()->{
                System.out.println("直接使用lambda表达式，实现Runnable接口");
        });
        thread5.start();

        //6.使用Callable接口
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int count = 0;
                for(int i=0;i<5000;i++) {
                    count++;
                }
                return count;
            }
        };
        FutureTask futureTask = new FutureTask(callable);
        Thread t = new Thread(futureTask);
        t.start();
        t.join();
        System.out.println("count = "+futureTask.get());





    }
}
